package com.burgeon.backend.verification;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.charset.StandardCharsets;

/**
 *
 * @Description:发送验证码
 */

public class GetMessageCode {
    private static final String QUERY_PATH = "http://sp.1xinxi.cn:7862/sms";

    // 根据相应的手机号发送验证码
    public static void getCode(String mobile,String rod) throws Exception {

        OutputStreamWriter out = null;
        BufferedReader br = null;
        StringBuilder result = new StringBuilder();

            URL url = new URL(QUERY_PATH);
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setRequestMethod("POST");
            connection.setDoInput(true);// 设置是否允许数据写入
            connection.setDoOutput(true);// 设置是否允许参数数据输出
            connection.setConnectTimeout(5000);// 设置链接响应时间
            connection.setReadTimeout(10000);// 设置参数读取时间
            connection.setRequestProperty("Content-type", "application/x-www-form-urlencoded");
            // 提交请求
            out = new OutputStreamWriter(connection.getOutputStream(), StandardCharsets.UTF_8);
            Query query = new Query();
            String data = query.getQuery(mobile,rod);

            out.write(data);
            out.flush();
            // 读取返回参数
            br = new BufferedReader(new InputStreamReader(connection.getInputStream(), "UTF-8"));
            String temp = "";
            while ((temp = br.readLine()) != null){
                result.append(temp);
            }
        System.err.println(result);
    }

    public static void main(String[] args) {

        try {
          GetMessageCode.getCode("15175137812","www.baidu.com");
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

}

